//
//  XOWMBindViewController.m
//  XOWMSDK
//
//  Created by skyline on 2018/7/27.
//  Copyright © 2018年 skyline. All rights reserved.
//

#import "XOWMBindViewController.h"
#import "XOWMBindView.h"
#import "XOWMRequest.h"
@interface XOWMBindViewController ()<YJDBindViewDelegate,YJDRequestDelegate>
@property (nonatomic, strong) XOWMBindView *bindView;
@property (nonatomic, strong) XOWMRequest *request;
@property (nonatomic, copy) NSString *code;
@property (nonatomic, assign) YJDBindViewEvent event;
@end

@implementation XOWMBindViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (XOWMBindView *)bindView
{
    if (!_bindView)
    {
        _bindView = [[XOWMBindView alloc] init];
        _bindView.delegate = self;
        [self.view addSubview:_bindView];
        _bindView.c_center.equal(self.view);
        _bindView.c_size.equal(self.bindView.bounds.size);
    }
    return _bindView;
}

- (XOWMRequest *)request
{
    if (!_request)
    {
        _request = [[XOWMRequest alloc]init];
        _request.delegate = self;
    }
    return _request;
}

#pragma mark - YJDBindViewDelegate
- (void)yjd_actionForYJDBindViewWithEvent:(YJDBindViewEvent)event dict:(NSDictionary *)dict
{
    self.event = event;
    if (event == YJDBindViewEventIgnore)
    {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
    else if (event == YJDBindViewEventVerify)
    {
        if ([self.bindView.phoneTF.text length] < 1) return [CCAlert showMessage:@"手机号码不能为空" hidden:nil];
        else if (![self.bindView.phoneTF.text yjd_isMobile]) return [CCAlert showMessage:@"手机号码有误" hidden:nil];
        [self.request yjd_verifyCodeRequest:dict];
        return;
    }
    else if (event == YJDBindViewEventBind)
    {
        if ([self.bindView.phoneTF.text length] < 1) return [CCAlert showMessage:@"手机号码不能为空" hidden:nil];
        else if (![self.bindView.phoneTF.text yjd_isMobile]) return [CCAlert showMessage:@"手机号码有误" hidden:nil];
        else if ([self.bindView.verifyTF.text length] < 1) return [CCAlert showMessage:@"验证码不能为空" hidden:nil];
        else if (![self.bindView.verifyTF.text isEqualToString:self.code]) return[CCAlert showMessage:@"验证码有误" hidden:nil];
        [CCHud startHud];
        [self.request yjd_bindRequest:dict account:self.accountid];
    }
}

#pragma mark - YJDRequestDelegate
- (void)yjd_didRequestWithReceiveData:(NSDictionary *)dict
{
    //关闭菊花
    [CCHud stopHud];
    
    if (!dict)
        return;
    
    int code = [[dict valueForKey:@"code"] intValue];
    NSString *info = [dict valueForKey:@"info"];
    //成功
    if (code == 100)
    {
        if (self.event == YJDBindViewEventVerify)
        {
            self.code = info;
            return;
        }
        helperManager().setupBindState(self.bindView.phoneTF.text);
        [CCAlert showMessage:info hidden:^{
                [self dismissViewControllerAnimated:YES completion:nil];
        }];
    }
    //失败
    else
    {
        [CCAlert showMessage:[NSString stringWithFormat:@"%@" ,info] title:@"错误" enter:nil];
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.bindView.backBtn.hidden = YES;
    self.bindView.titleIMG.content = (helperManager().getPara().style == XOWMPlatformStyleImage)?@"yjd_绑定手机_title":@"绑定手机";
    self.bindView.phoneTF.placeholder = @"请输入手机号码";
    self.bindView.verifyTF.placeholder = @"获取验证码";
    self.bindView.ignoreBtn.content = (helperManager().getPara().style == XOWMPlatformStyleImage)?@"yjd_绑定取消_Btn":@"下次再说";
    self.bindView.bindBtn.content = (helperManager().getPara().style == XOWMPlatformStyleImage)?@"yjd_绑定确定_Btn":@"确定";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */


- (void)sp_getUsersMostFollowerSuccess:(NSString *)isLogin {
    NSLog(@"Get User Succrss");
}
@end
